<?php
/**
 * Handles the access to certain areas within the application
 *
 * @package System
 * @version $Id:$
 */

/**
 * Handles the access to certain areas within the application
 * 
 * Access Rules
 * 1 - Root User
 * 2 - Site Admin
 * 4 - Guest (not logged in)
 * 8 - Standard User (logged in)
 * 16 - Expert User (can answer questions)
 * 
 * @package System
 * @subpackage System_Security
 * @author Mark Henderson <emehrkay@gmail.com>
 */
class System_Security_Rules implements System_Registry_Interface {
    /**
     * holds a boolean state of the check
     * 
     * @var boolean defaults to true
     * @access private
     */
    private $_passed = true;
    
    /**
     * holds the access level to be checked
     *
     * @var boolean|integer
     * @access private
     */
    private $_access_level = false;
    
    /**
     * Class Constructor
     *
     * @access public
     * @return null
     */     
    public function __construct($access_level = false){
        $this->setValue($access_level);
    }
    
    /**
     * reset
     *
     * @access public
     * @return object, instance of this class
     */
    public function reset(){
        $this->_access_level = false;
        $this->_passed       = true;
        
        return $this;
    }
    
    /**
     * sets the page's access level to be checked
     * inherited from the system_registry_interface
     *
     * @access public
     * @return object, instance of this class
     */
    public function setValue($access_level){
        if(is_numeric($access_level)){
            $this->_access_level = $access_level;
        }
        
        return $this;
    }
    
    /**
     * returns the access_level property
     *
     * @access public
     * @return boolean|integer
     */
    public function get($access_level = true){
        return $this->_access_level;
    }
    
    /**
     * returns a boolean result of the method checks    
     *
     * @access public
     * @return boolean
     */
    public function process(){
        return (bool) $this->_passed;
    } 
    
    /**
     * checks the access_level property against the second arguemnt
     *
     * @param integer $access_level, access level to be checked
     * @access public
     * @return System_Security_Rules
     */
    public function checkAnd($access_level){
        if($this->_passed){
            $this->_passed = $this->_access_level & $access_level;
        }
        
        return $this;
    }
    
    /**
     * checks to see if the user is a root user or & 1
     *
     * @access public
     * @return System_Security_Rules
     */
    public function isRoot(){
        if($this->_passed){
            $this->_passed = $this->_access_level & 1;
        }
        
        return $this;
    }
    
    /**
     * Checks to see if the user is an expert (16)
     * @access public
     * @return System_Security_Rules
     */
    public function isExpert(){
        if($this->_passed){
            $this->_passed = $this->_access_level & 16;
        }
        
        return $this;
    }

    /**
     * Checks to see if the user is an Admin in the system (2)
     * @access public
     * @return System_Security_Rules
     */
    public function isAdmin(){
        if($this->_passed){
            $this->_passed = $this->_access_level & 2;
        }
        
        return $this;
    }

    
    
}